from textbook_expert.service.SupabaseService import SupabaseService
from textbook_expert.model.Quiz import Quiz,BasicInfo,Question

class QuizService:
    def __init__(self):
        self.supabse = SupabaseService()

    def add_quiz_json(self, basic_info, questions):
        return self.supabse.client.table('quiz').insert([{'basic_info': basic_info, 'content': questions}]).execute()


    def add_quiz(self, quiz:Quiz):
        basic_info = quiz.basic_info.model_dump_json()
        content = [q.model_dump_json() for q in quiz.content]
        return self.supabse.client.table('quiz').insert([{'basic_info': basic_info, 'content': content}]).execute()
    
    
if __name__ == '__main__':
    quizService = QuizService()
    bi_json = {'chapter_title': '第一章管理的历史发展', 'section_title': '第一节古典管理理论', 'subsection_title': '一、早期的管理思想', 'subsection_number': 1}
    bi = BasicInfo(**bi_json)
    question_json = [{'question': '以下哪项是企业的根本特征之一？', 'options': ['企业的目的是获得不断增加盈利。', '企业是政府行政管理机构的附属物。', '企业没有义务无偿承担各种非企业性职能。', '企业是一个经济组织，而且是独立的经济组织。'], 'correctAnswerIndex': 0, 'explanation': '企业的根本特征之一是获得不断增加盈利，自负盈亏，是企业得以不断发展的内在要求。'}]
    content = [Question(**q) for q in question_json]
    quiz = Quiz(basic_info=bi, content=content)
    quizService.add_quiz(quiz)
    print('ok')